package pwd.allen.androiddemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.danikula.videocache.HttpProxyCacheServer;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import pwd.allen.androiddemo.MyApplication;
import pwd.allen.androiddemo.R;

/**
* 使用JiaoZiVideoPlayer实现视频播放
*/
public class VideoPlayerActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout rlParent;
    private JzvdStd videoPlayer;
    private String path;

    public static void actionStart(Context context,String path){
        Intent intent = new Intent(context,VideoPlayerActivity.class);
        intent.putExtra("path", path);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        path = getIntent().getStringExtra("path");
        path = "https://vali-g1.cp31.ott.cibntv.net/youku/677403e64dd4171c6db66489b/030008010063D20BD80B398C6117AC80B84A60-B031-4850-A911-0953C643A5FE.mp4?sid=167678493800010007325_00_Ba59fdfd346dbe8c9f6b94075af8008f1&sign=5a1c0118397b265217141fa7cf8e9c2c&ctype=50&si=183&psid=b77e5b17b1317c42ce1df4ba900283cf41346";
        if (getIntent().hasExtra("path")) {
            path = getIntent().getStringExtra("path");
        }
        HttpProxyCacheServer proxy = MyApplication.getProxy(this);
        path = proxy.getProxyUrl(path);
        Log.i("mennalisha", path);
        init();
    }

    private void init(){
        rlParent = findViewById(R.id.rl_parent);
        rlParent.setOnClickListener(this);
        videoPlayer = findViewById(R.id.video_player);

        // 设置缩略图
        Glide.with(this).load(R.drawable.ting1).into(videoPlayer.thumbImageView);

        if(!TextUtils.isEmpty(path)){
            // 设置视频地址、播放模式、标题等
            videoPlayer.setUp(path, "标题", JzvdStd.SCREEN_NORMAL);

            // 设置自动播放，两种方式选其中一个
//            videoPlayer.startButton.performClick();
            videoPlayer.startVideo();

        }
        // 设置点击左上角返回按钮退回
        videoPlayer.backButton.setOnClickListener(this);

        // 设置不保存播放进度
        Jzvd.SAVE_PROGRESS = false;
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void onDestroy() {
        try {
            Jzvd.resetAllVideos();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
